package com.lplb.api.admin.service.impl.rpc;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lplb.api.admin.mapper.AdminAuthMapper;
import com.lplb.api.admin.mapper.AdminRoleAuthMapper;
import com.lplb.api.admin.model.dto.AdminAuthDTO;
import com.lplb.api.admin.model.entity.AdminAuth;
import com.lplb.api.admin.model.entity.AdminRoleAuth;
import com.lplb.api.admin.model.vo.AdminAuthVO;
import com.lplb.api.admin.service.rpc.RIAdminAuthService;
import com.lplb.common.core.result.R;
import com.lplb.common.core.service.BaseIServiceImpl;
import com.lplb.common.core.utils.BeanDtoVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 资源表 PRC 服务接口实现
 * </p>
 *
 * @author ws
 * @date 2020-07-21
 */
@RestController
public class RAdminAuthServiceImpl extends BaseIServiceImpl<AdminAuthMapper, AdminAuth> implements RIAdminAuthService {

    @Autowired
    private AdminRoleAuthMapper adminRoleAuthMapper;

    /**
     *  查询所有
     */
    @Override
    public R<List<AdminAuthVO>> findList() {
        List<AdminAuth> list = this.list(new LambdaQueryWrapper<AdminAuth>()
                .orderByAsc(AdminAuth::getCreateTime)
        );
        return R.success(BeanDtoVoUtil.listVo(list, AdminAuthVO.class));
    }


    /**
     *  添加/刷新/删除无用权限
     */
    @Override
    public R<Void> updList(@RequestBody List<AdminAuthDTO> adminAuthDTO) {
        if (adminAuthDTO == null || adminAuthDTO.size() == 0) {
            return R.success();
        }
        //原数据
        Map<String, AdminAuth> oldAuthMap = new HashMap<>();
        List<AdminAuth> authList = this.list();
        for (AdminAuth adminAuth : authList) {
            oldAuthMap.put(adminAuth.getServerName() + adminAuth.getUrl(), adminAuth);
        }
        // 需要添加/更新
        List<AdminAuth> addAuthList = new ArrayList<>();
        List<AdminAuth> updAuthList = new ArrayList<>();
        for (AdminAuthDTO dto : adminAuthDTO) {
            if (oldAuthMap.containsKey(dto.getServerName() + dto.getUrl())) {
                // 需要更新，获取后从原数据清除，最后剩下的将是多余数据并删除
                AdminAuth updAdminAuth = oldAuthMap.get(dto.getServerName() + dto.getUrl());
                updAdminAuth.setDesc(dto.getDesc());
                updAdminAuth.setUrl(dto.getUrl());
                updAdminAuth.setServerName(dto.getServerName());
                updAuthList.add(updAdminAuth);
                oldAuthMap.remove(dto.getServerName() + dto.getUrl());
            } else {
                // 需要添加
                addAuthList.add(dto.convert(AdminAuth.class));
            }
        }
        // 需要删除
        List<String> delAuthIds = new ArrayList<>();
        for (String key : oldAuthMap.keySet()) {
            delAuthIds.add(oldAuthMap.get(key).getId());
        }
        // 数据操作
        if (addAuthList.size() > 0) {
            this.saveBatch(addAuthList);
        }
        if (updAuthList.size() > 0) {
            this.updateBatchById(updAuthList);
        }
        if (delAuthIds.size() > 0) {
            this.removeByIds(delAuthIds);
            //删除角色权限关联数据
            adminRoleAuthMapper.delete(new LambdaQueryWrapper<AdminRoleAuth>().in(AdminRoleAuth::getAuthId, delAuthIds));
        }
        return R.success();
    }
}

